<template>
  <el-dialog
    :title="view === 'create'? '新增': '编辑'"
    :visible="true"
    custom-class="dialogWrapper"
    :before-close="closeDialog">
    <el-form :model="formData" label-width="80px" ref="ruleForm" :rules="rules">
      <el-form-item label="用户名称" prop="userName" required>
        <el-input v-model="formData.userName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" v-if="view==='create'" prop="pwd" required>
         <el-input v-model="formData.pwd" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户角色"  prop="userRole" required>
        <el-select v-model="formData.roleId" >
         <el-option
           v-for="(item, index) in roles"
           :label="item.roleName"
           :value="item.id"
           :key="index">
         </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户描述" prop="userDesc" required>
        <el-input
          v-model="formData.userDesc"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
         auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit('ruleForm')" :disabled="Subabled">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {create, queryRole, edit} from '@/services/users'
  import {RequestError} from '@/services/errors'
  export default {
    name: 'edit-user',
    data: () => ({
      formData: {
        userName: '',
        pwd: '',
        userDesc: '',
        roleId: '',
        roleType: ''
      },
      Subabled:false,
      roles: [],
      rules: {
        userName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        userDesc: [
          { required: true, message: '请输入用户描述', trigger: 'blur' }
        ]
      }
    }),
    props: {
      view: {
        type: String,
        default: 'create'
      }
    },
    methods: {
      async queryRoles () {
        try {
          this.roles = await queryRole()
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
      },
      closeDialog () {
        this.$emit('close')
      },
      submit (formName) {
      	this.Subabled = true;
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            try {
              if (this.view === 'create') {
                await create({
                  userName: this.formData.userName,
                  pwd: this.formData.pwd,
                  userDesc: this.formData.userDesc,
                  userRole: String(this.formData.roleId)
                })
              } else {
                await edit({
                  id: this.formData.id,
                  userName: this.formData.userName,
                  pwd: this.formData.pwd,
                  userRole: this.formData.roleId,
                  userDesc: this.formData.userDesc
                })
              }
              this.$emit('close', true)
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
              this.Subabled = false;
            } catch (e) {
              if (e instanceof RequestError) {
                this.$alert(e.message, '提示', {
                  type: 'error',
                  confirmButtonText: '确定'
                })
                this.Subabled = false;
              } else {
                throw e
              }
            }
          } else {
            return
          }
        })
      },
      closes() {
      }
    },
    created () {
      this.queryRoles()
    }
  }
</script>
<style>
  .dialogWrapper {
    width: 540px;
  }
  .el-select {
    width: 100%;
  }
</style>
